import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

Vue.config.productionTip = false

// 全局设置
import global from './global'
Vue.use(global)

// 全局组件
import MyList from "components/common/myList/MyList"
Vue.component(MyList.name, MyList)
import MyMenu from "components/common/myMenu/MyMenu"
Vue.component(MyMenu.name, MyMenu)
// import MyLogin from 'components/common/myLogin/MyLogin';
// Vue.component(MyLogin.name, MyLogin);
// import MyRegister from 'components/common/myregister/MyRegister';
// Vue.component(MyRegister.name, MyRegister);


// 网络请求
import 'network/request'


// 全局拦截器，路由守卫，在进入用户权限的页面前校验是否已经登陆
router.beforeResolve((to, from, next) => {
  const loginUser = store.state.user.user;
  // 判断路由是否设置相应校验用户权限
  if (to.meta.requireAuth) {
    if (!loginUser) {
      // 没有登陆，显示登陆组件
      store.dispatch('setShowLogin', true)
      if (from.name == null) {
        //此时，是在页面没有加载，直接在地址栏输入链接，进入需要登录验证的页面
        next("/");
        return;
      }
      // 终止导航
      next(false);
      return;
    }
  }
  next();
})

// 时间格式化
Vue.filter("dateFormat", dateStr => {
  let time = new Date(dateStr)

  function timeAdd0(param) {
    if (param < 10) {
      return "0" + param
    }
    return param
  }

  let y = time.getFullYear()
  let m = time.getMonth() + 1
  let d = time.getDate()
  let h = time.getHours()
  let M = time.getMinutes()
  let s = time.getMinutes()

  return y + '-' + timeAdd0(m) + '-' + timeAdd0(d) + ' ' + timeAdd0(h) + ':' + timeAdd0(M) + ':' + timeAdd0(s);
})


new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')